package com.hqully.labmanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.labmanage.common.enums.ResultEnum;
import com.hqully.labmanage.common.exception.DAOException;
import com.hqully.labmanage.common.exception.ResultException;
import com.hqully.labmanage.domain.entity.Student;
import com.hqully.labmanage.domain.dto.StudentDTO;
import com.hqully.labmanage.domain.dto.PageDTO;
import com.hqully.labmanage.service.StudentService;
import com.hqully.labmanage.mapper.StudentMapper;
import com.hqully.labmanage.service.impl.constant.MapperConst;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 针对数据库表[student]的操作Service
 * </p>
 *
 * @entity {@link Student}
 * @date 2022-10-08 10:05:24 <br>
 * @author hqully <br>
 * @version 1.0
 */
@Slf4j
@Service
public class StudentServiceImpl implements StudentService {


    @Resource
    private StudentMapper studentMapper;

    @Override
    public PageInfo<StudentDTO> pageStudents(PageDTO page, StudentDTO studentDTO) {
        try {
            PageHelper.startPage(page);
            List<StudentDTO> studentList = studentMapper.listStudents(studentDTO);
            return new PageInfo<>(studentList);
        } catch (Exception e) {
            log.error("PAGE [student] FAIL\nINPUT OBJECT: {}\nREASON: {}", studentDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询学生分页列表失败");
        }
    }

    @Override
    public List<StudentDTO> listStudents(StudentDTO studentDTO) {
        try {
            return studentMapper.listStudents(studentDTO);
        } catch (Exception e) {
            log.error("LIST [student] FAIL\nINPUT OBJECT: {}\nREASON: {}", studentDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询学生列表失败");
        }
    }

    @Override
    public int addStudent(StudentDTO studentDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO, student);

        int status;
        try {
            status = studentMapper.addStudent(student);
        } catch (DuplicateKeyException e) {
            log.warn("ADD [student] FAIL\nINPUT OBJECT: {}\nREASON: {}", studentDTO, e.toString());
            throw new ResultException(ResultEnum.DUPLICATE_KEY_CONFLICT, "添加学生失败\n原因: 学生id已存在");
        } catch (Exception e) {
            log.error("ADD [student] FAIL\nINPUT OBJECT: {}\nREASON: {}", studentDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "添加学生失败");
        }
        return status;
    }

    @Override
    public int updateStudent(StudentDTO studentDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO, student);

        int status;
        try {
            status = studentMapper.updateStudent(student);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [student] record not exist");
            }
        }catch (DAOException e){
            log.warn("UPDATE [student] FAIL\nINPUT OBJECT: {}\nREASON: {}", studentDTO, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "更新学生失败\n原因: 学生不存在");
        }
        catch (Exception e) {
            log.error("UPDATE [student] FAIL\nINPUT OBJECT: {}\nREASON: {}", studentDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "更新学生失败");
        }
        return status;
    }

    @Override
    public int deleteStudentById(Integer id) {
        int status;
        try {
            status = studentMapper.deleteStudentById(id);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [student] record not exist");
            }
        } catch (DAOException e) {
            log.warn("DELETE [student] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "删除学生失败\n原因: 学生不存在");
        } catch (Exception e) {
            log.error("DELETE [student] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.ERROR, "删除学生失败");
        }
        return status;
    }


}



